from custom.handlers.basehandler import BaseRequestHandler
import requests
from urllib import parse
import uuid
import os
from settings.config import config

title_map = {
    'live': '学生端',
    'anchor': '讲师端',
    'replay': '回放',
    'lead': '引导页',
    'index': '主页index',
    'switzerlandList': '机构主页',
    'doReplay': '录制素材',
    'replayPreview': '预览素材',
}

class IndexHandler(BaseRequestHandler):
    def get(self, *args, **kwargs):
        page = self.get_argument('page', None)
        if page is None:
            self.success(-200, '参数错误')
        page_path = ''
        text = ''
        if page == 'live' or page == 'anchor' or page == 'replay' or page == 'lead':
            room = self.get_argument('room', None)
            if room is None:
                self.success(-200, '参数错误')
            test = self.get_argument('test', 0)
            page_path = 'pages/' + page + '?scene=room%3D' + room + '%23test%3D' + test

        if page == 'doReplay' or page == 'replayPreview':
            room = self.get_argument('room', None)
            material = self.get_argument('material', None)
            if room is None or material is None:
                self.success(-200, '参数错误')
            test = self.get_argument('test', 0)
            page_path = 'pages/' + page + '?scene=room%3D' + room + '%23test%3D' + test + '%23material%3D' + material

        if page == 'index':
            page_path = 'pages/index'
        if page == 'switzerlandList':
            agency = self.get_argument('agency', None)
            if agency is None :
                self.success(-200, '参数错误')
            test = self.get_argument('test', 0)
            page_path = 'pages/switzerlandList?scene=agency%3D' + agency + '%23test%3D' + test
        base_url = 'https://mp.weixin.qq.com/wxopen/basicprofile?'
        search_data = {
            'action': 'get_qrcode',
            'type': 1,
            'openid': 'oux_M4heXxJWK0xD3jiqMpfZVqws',
            'use_path': 1,
            'path': page_path,
            'token': '329606997',
            'lang': 'zh_CN'
        }
        default_header = {
            'Accept': 'image/webp,image/apng,image/*,*/*;q=0.8',
            'Accept-encoding': 'gzip, deflate, br',
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
            'Cookie': config.COOKIE
        }
        real_url = base_url + parse.urlencode(search_data)
        req = requests.Request('GET', real_url,
                               data=None,
                               headers=default_header)
        prepped = req.prepare()
        _session = requests.session()
        # resp = self.session.send(prepped, verify=False, proxies=self.proxy)
        resp = _session.send(prepped)
        qr_url = self.save_image(resp.content)
        self.render('index.html', qr_url=qr_url, title=title_map.get(page))

    @staticmethod
    def save_image(content):
        if not os.path.exists(os.path.join(config.APP_ROOT, 'uploads')):
            os.mkdir(os.path.join(config.APP_ROOT, 'uploads'))
        file_name = str(uuid.uuid4()) + '.jpg'
        real_path = os.path.join(config.APP_ROOT, 'uploads/' + file_name)
        upload_file = open(real_path, "wb+")
        upload_file.write(content)
        upload_file.close()
        return '/uploads/' + file_name
